package com.example.pufang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.PageParamRequest;
import com.example.pufang.mapper.AdminMapper;
import com.example.pufang.pojo.Admin;
import com.example.pufang.pojo.User;
import com.example.pufang.service.AdminService;
import com.example.pufang.vo.PageListVo;
import com.example.utils.DESUtils;
import com.example.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private TokenUtil tokenUtil;

    @Override
    public Map<String, Object> login(Admin admin) {

        HashMap<String, Object> map = new HashMap<>();

        String encryptResult = DESUtils.encrypt(admin.getPassword());

        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername, admin.getUsername())
                        .eq(Admin::getPassword, DESUtils.encrypt(admin.getPassword()));

        Admin result = baseMapper.selectOne(wrapper);

        // 密码校验成功，生成随机 uuid 作为 token 存入 redis
        if (ObjectUtils.isNotNull(result)) {
            String adminToken = tokenUtil.createAdminToken(result);
            map.put("token", adminToken);
            return map;
        }
        return null;
    }

    @Override
    public PageListVo getListByPage(PageParamRequest pageParamRequest, String searchName) {
        PageListVo pageListVo = new PageListVo();

        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(searchName), Admin::getUsername, searchName);

        Page<Admin> page = new Page<>(pageParamRequest.getPage(), pageParamRequest.getLimit());
        IPage<Admin> adminIPage = baseMapper.selectPage(page, wrapper);

        pageListVo.setPage(adminIPage.getCurrent())
                .setSize(adminIPage.getSize())
                .setList(JSONObject.toJSONString(adminIPage.getRecords()))
                .setTotal(adminIPage.getTotal())
                .setTotalPage(adminIPage.getPages());

        return pageListVo;
    }

    /**
     * 封禁用户
     * @param admin
     * @return
     */
    @Override
    public Boolean ban(Admin admin) {

        Admin databaseAdmin = baseMapper.selectById(admin.getId());
        if (ObjectUtils.isNull(databaseAdmin)) {
            return false;
        } else {
            databaseAdmin.setIsBan(true);
        }
        return baseMapper.updateById(databaseAdmin) > 0 ? true : false;
    }

    /**
     * 解封用户
     * @param admin
     * @return
     */
    @Override
    public Boolean cancelBan(Admin admin) {

        Admin databaseAdmin = baseMapper.selectById(admin.getId());

        if (ObjectUtils.isNull(databaseAdmin)) {
            return false;
        } else {
            databaseAdmin.setIsBan(false);
        }

        return baseMapper.updateById(databaseAdmin) > 0 ? true : false;
    }


    @Override
    public Boolean add(Admin admin) {

        admin.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif")
                .setCreateTime(new Date())
                .setRoles(2)
                .setPassword(DESUtils.encrypt(admin.getPassword()));
        return this.save(admin);
    }

    @Override
    public Admin getAdminByName(String username) {

        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(username), Admin::getUsername, username);

        return baseMapper.selectOne(wrapper);
    }
}
